<template>
  <!--增加记录  -->
  <div class="add-record">
    <div class="add-record-title blueTitleStyle">增加记录</div>
    <div class="add-record-content">
      <div class="delete-box endbox">
        <el-button type="primary" @click="handleDelete">删除</el-button>
        <el-button type="primary" @click="handleDeleteAll">全部删除</el-button>
      </div>
      <div class="table-group bigTableStyle">
        <el-table

        border
          :data="tableData"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
          style="width: 100%"
          height="100%"
          v-loading="loading"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.5)"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column type="index" width="55" label="序号"></el-table-column>
          <el-table-column prop="stationName" label="变电站名称" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="areaName" label="区域房间" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="pointName" label="门禁" :show-overflow-tooltip="true"></el-table-column>
        </el-table>
      </div>
      <div class="filtrate-group centerbox">
        <el-form ref="form" :inline="true" :model="form" label-width="1rem">
          <el-row>
            <el-col :span="10">
              <el-form-item label="被授权人:">
                <el-select v-model="form.authorPersonVal" placeholder="请选择">
                  <el-option
                    v-for="item in authorPerson"
                    :key="item.value"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="14">
              <el-form-item label="联动时间：">
                <el-date-picker
                  :style="{width: '100%'}"
                  v-model="form.strtimeedntime"
                  type="daterange"
                  align="right"
                  unlink-panels
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="钥匙编号:">
                <el-select v-model="form.keyNumVal" placeholder="请选择">
                  <el-option
                    v-for="item in keyNum"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="14">
              <div class="endbox btn">
                <el-button type="primary" @click="handleSave">保存</el-button>
                <el-button type="primary" @click="handleCancel">取消</el-button>
                <el-button type="primary" @click="handleAuth">授权</el-button>
              </div>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { objType } from "@/utils/interface";
import { LocksAPIModule } from "@/store/modules/locks";

@Component
export default class AddRecord extends Vue {
  //   表格
  tableData: Array<object> = [];
  // 表单
  public form = {
    authorPersonVal: "",
    strtimeedntime: [],
    keyNumVal: "",
  };
  loading: boolean = true;
  tableSelect = [];
  get authorPerson() {
    return LocksAPIModule.getUserList;
  }
  get keyNum() {
    return LocksAPIModule.getDeviceItemList;
  }

  mounted() {
    this.fetch();
  }

  // 增加记录-查询
  fetch() {
    var params = {
      stationId: "",
    };
    (this as any).$services
      .getIncreateList({ data: params })
      .then((res: any) => {
        // console.log(res.data);
        if (res.data) {
          this.tableData = res.data;
          this.loading = false;
        }
      })
      .catch((err: object) => {
        console.log(err);
        this.loading = false;
      });
  }

  // 表格隔行变色
  tableRowClassName({ row, rowIndex }: objType): string {
    // console.log(row, rowIndex);
    if (rowIndex % 2 === 0) {
      return "row1";
    } else {
      return "row2";
    }
  }
  handleSelectionChange(val: any): void {
    // console.log(val);
    this.tableSelect = val;
  }

  // 保存-增加记录提交授权接口
  handleSave() {
    if (this.tableSelect.length !== 1) {
      this.$message({
        message: "请选择一行数据",
        type: "warning",
      });
    } else {
      // console.log(this.tableSelect);
      // console.log(this.form);

      var params = {
        accessId: this.tableSelect[0].pointId,
        accessName: this.tableSelect[0].pointName,
        areaId: this.tableSelect[0].areaId,
        areaName: this.tableSelect[0].areaName,
        authPerson: "",
        authendTime: this.form.strtimeedntime[1] || "",
        authstartTime: this.form.strtimeedntime[0] || "",
        bauthPerson: this.form.authorPersonVal || "",
        keyCode: this.form.keyNumVal || "",
        stationId: this.tableSelect[0].stationId,
        stationName: this.tableSelect[0].stationName,
      };
      (this as any).$services
        .insertSubmitAuth({ data: params, method: "post" })
        .then((res: any) => {
          // console.log(res.data);
          if (res.data) {
            this.$emit("handleAddtodo");
            this.$message({
              type: "success",
              message: res.message,
            });
          } else {
            this.$message({
              type: "warning",
              message: res.msg,
            });
          }
        })
        .catch((err: object) => {
          console.log(err);
        });
    }
  }
  // 取消
  handleCancel() {
    if (this.tableSelect.length !== 1) {
      this.$message({
        message: "请选择一行数据",
        type: "warning",
      });
    } else {
      // console.log(this.tableSelect);
    }
  }
  // 授权-增加记录授权接口
  handleAuth() {
    if (this.tableSelect.length !== 1) {
      this.$message({
        message: "请选择一行数据",
        type: "warning",
      });
    } else {
      // console.log(this.tableSelect);
      // console.log(this.form);

      var params = {
        accessId: this.tableSelect[0].pointId,
        accessName: this.tableSelect[0].pointName,
        areaId: this.tableSelect[0].areaId,
        areaName: this.tableSelect[0].areaName,
        authPerson: "",
        authendTime: this.form.strtimeedntime[1] || "",
        authstartTime: this.form.strtimeedntime[0] || "",
        bauthPerson: this.form.authorPersonVal || "",
        keyCode: this.form.keyNumVal || "",
        stationId: this.tableSelect[0].stationId,
        stationName: this.tableSelect[0].stationName,
      };
      (this as any).$services
        .insertAuth({ data: params, method: "post" })
        .then((res: any) => {
          // console.log(res.data);
          if (res.data) {
            this.$emit("handleAddauth");
            this.$message({
              type: "success",
              message: res.message,
            });
          } else {
            this.$message({
              type: "warning",
              message: res.msg,
            });
          }
        })
        .catch((err: object) => {
          console.log(err);
        });
    }
  }

  handleDelete() {
    if (this.tableSelect.length !== 1) {
      this.$message({
        message: "请选择一行数据",
        type: "warning",
      });
    } else {
      // console.log(this.tableSelect);
    }
  }
  handleDeleteAll() {
    if (this.tableSelect.length < 1) {
      this.$message({
        message: "请至少选择一行数据",
        type: "warning",
      });
    } else {
      // console.log(this.tableSelect);
    }
  }
}
</script>

<style lang="scss" scoped>
.add-record {
  width: 100%;
  height: 100%;
  position: relative;
  &-title {
    width: 100%;
    height: 0.26rem;
  }
  &-content {
    width: 100%;
    height: calc(100% - 0.26rem);
    .delete-box {
      position: absolute;
      top: 0;
      left: 0;
      height: 0.26rem;
      width: 100%;
    }
    .table-group {
      width: 100%;
      height: calc(100% - 0.9rem);
      padding: 0.1rem;
      box-sizing: border-box;
      overflow: auto;
    }
    .filtrate-group {
      width: 100%;
      height: 0.9rem;
      .btn {
        padding-right: 0.1rem;
      }
    }
  }
}
</style>
<style lang="scss">
.add-record {
  // 按钮的高度

  .delete-box {
    .el-button--primary {
      height: 0.26rem;
    }
    .el-button {
      padding: 0 0.15rem;
    }
  }
  // 时间
  .el-input--prefix {
    .el-input__inner {
      height: 0.26rem;
    }
    .el-input__icon {
      line-height: 0.26rem;
    }
  }
  .el-input--suffix .el-input__inner {
    padding-right: 0.1rem;
  }
  // input框的高度{
  .el-select .el-input .el-input__inner {
    height: 0.26rem;
  }
  .el-form-item {
    margin-bottom: 0.1rem;
  }
  .el-form-item__content {
    width: calc(100% - 1rem);
  }
}
</style>